package com.ahm.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.annotation.Resource;

/*功能描述
 * @author ahm
 */
@Configuration
public class LoginConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new MyPasswordEncoder();
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.headers().frameOptions().disable();

        http.csrf()
                .disable()
                .authorizeRequests()
                .antMatchers("/login","/index", "/login/**", "/logout/**").permitAll()
                                .antMatchers("/index").permitAll()
                .antMatchers("/login.ftl").permitAll()
                .antMatchers("/views/login.ftl").permitAll()
                .antMatchers("/login").permitAll()
                .antMatchers("/js/**","/css/**").permitAll()
                .antMatchers("/images/**").permitAll()
                .antMatchers("/lib/**").permitAll()
                .anyRequest().authenticated()
                .and().formLogin().loginPage("/index").loginProcessingUrl("/login")
                .usernameParameter("username")
                .passwordParameter("password")
                .successHandler(new MyAuthenticationSuccessHandler()).permitAll()
                .failureHandler(new MyAuthenticationFailureHandler());
    }


}